package com.sznaner.tool.util;


import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class JSONUtil {

    public static void saveJsonStrToSdCard_async(final String path, final String jsonStr, final String fileName) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //需要在子线程中处理的逻辑
               saveJsonStrToSdCard(path,jsonStr,fileName);
            }
        }).start();
    }

    public static void saveJsonStrToSdCard(String path,String jsonStr,String fileName) {
        File file = new File(path + File.separator + fileName+".json");
        // 文件夹不存在的话，就创建文件夹
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        // 写入内存卡
        PrintStream outputStream = null;
        try {
            outputStream = new PrintStream(new FileOutputStream(file));
            outputStream.print(jsonStr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

}
